//
// Description: 5930. 两栋颜色不同且距离最远的房子
// Created by Loading on 2021/11/21.
//

#include <bits/stdc++.h>

using namespace std;

// 贪心
int maxDistance(vector<int> &colors) {
    int n = colors.size();
    if (colors[0] != colors[n - 1]) {
        return n - 1;
    }
    int c = colors[0];
    int res = 0;
    for (int i = 1; i < n - 1; ++i) {
        if (colors[i] != c) {
            res = max(res, n - 1 - i);
        }
    }
    for (int i = n - 1; i >= 1; --i) {
        if (colors[i] != c) {
            res = max(res, i);
        }
    }

    return res;
}

int main() {
    vector<int> colors = {1, 1, 1, 6, 1, 1, 1};
    int res = maxDistance(colors);
    cout << res << endl;

    return 0;
}